# -*- coding:utf-8 -*-
# Create time:2020/3/13 11:12:19
# Author:Rongzhikai

import sys
import os

path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, path)
# 导入配置文件中定义的测试用例的路径
from conf.settings import TESTCASE_PATH
# 导入配置文件中定义的测试报告的路径
from conf.settings import REPORT_PATH
# 测试数据文件
from conf.settings import DATA_FILE, LOG_FILE
import unittest
# 导入报告模板
from libs.HTMLTestReportCN import HTMLTestRunner as hr2
from libs.iemail import send_report, new_report
from libs.global_vars import gvar
import time

# 自动根据测试用例的路径匹配查找测试用例文件（*.py）,并将查找到的测试用例组装到测试套件中
suit = unittest.defaultTestLoader.discover(TESTCASE_PATH, pattern='test_*.py')
if __name__ == '__main__':
    # 获取当前时间并指定时间格式
    now = time.strftime("%Y-%m-%d_%H_%M_%S")
    # 创建报告文件
    fp = open(r'%s\%s_report.html' % (REPORT_PATH, now), 'wb')
    runner = hr2(
        stream=fp,
        title=u'泰坦软件公司接口自动化测试报告',
        description=u'此次接口测试报告仅作参考，主要测试档案管理相关软件。',
        tester="rongzhikai")
    runner.run(suit)
    fp.close()

    # 执行完用例，最后发送最新的邮件报告
    report_file = new_report(REPORT_PATH)
    files = [DATA_FILE, report_file, LOG_FILE]
    # 计算用例执行情况
    total_cases = gvar.pass_count + gvar.fail_count
    if total_cases != 0:
        pass_percent = '%.2f%%' % (gvar.pass_count / total_cases * 100)
        fail_percent = '%.2f%%' % (gvar.fail_count / total_cases * 100)
    else:
        pass_percent = '0.00%'
        fail_percent = '0.00%'
    message = '此次一共运行接口测试用例数：%s\n' \
              '通过个数为：%s\n' \
              '失败个数为：%s\n' \
              '通过率为：%s\n' \
              '失败率为：%s\n' \
              '报告详情请查看附件内容。\n' % (total_cases, gvar.pass_count, gvar.fail_count, pass_percent, fail_percent)
    # send_report(message=message, files=files)
